#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MYPORT 3334
#define BACKLOG 10

int main()
{
	int sockfd, new_fd;
	struct sockaddr_in my_addr, their_addr;
	int sin_size;

	if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("Server-socket() error lol!");
		exit(1);
	}
	else
		printf("Server-socket() sockfd is OK\n");
	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(MYPORT);
	my_addr.sin_addr.s_addr = INADDR_ANY;
//	my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	memset(&my_addr.sin_zero, 0, 8);

	if (bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) < 0)
	{
		perror("Server-bind() error lol!");
		exit(1);
	}
	else
		printf("Server-bind() sockfd is OK\n");
	if (listen(sockfd, BACKLOG) < 0)
	{
		perror("Server-listen() error lol!");
		exit(1);
	}
	else
		printf("Server-listen() is OK\n");

	sin_size = sizeof(struct sockaddr_in);
	new_fd = accept(sockfd, (struct sockaddr*)&their_addr, &sin_size);
	if (new_fd < 0)
		perror("accept() error lol!");
	else
		printf("accept() is OK.\n");
		
{
	char buf[128];
	int len;
	recv(new_fd, buf, 128, 0);
	printf("%s\n", buf);
}
	close(new_fd);
	close(sockfd);
	return 0;
}
